package tests.ge;

import java.io.File;

import org.apache.log4j.Logger;

import tests.ge.GrammarTest.DumbIterator;
import ge.BNFGrammarParser;
import ge.CodeEvaluator;
import ge.Expression;
import ge.standardsymbols.StartSymbol;

public class BNFTester {
	static final Logger LOG = Logger.getLogger("Main");

	public static void main(String[] args) throws Exception {

		String grammar_file = "loop_grammar.txt";
		boolean evaluate = false;

		Class<Integer> return_type = Integer.class;
		String[] parameter_names = {};
		Class<?>[] parameter_types = {};

		LOG.debug("parsing grammar");

		BNFGrammarParser parser = new BNFGrammarParser(new File("src/tests/ge/"
				+ grammar_file), "tests.ge.GrammarTest$");
		System.out.println(parser);

		LOG.debug("creating sample expressions");
		for (int i = 0; i < 5; i++) {
			Expression exp = StartSymbol.resolve(new DumbIterator());

			if (exp != null) {
				System.out.println("Length=" + exp.getLength());
				System.out.println(exp);
				String code = exp.createInstance().getCode();
				System.out.println(code.replace("\\n", "\n"));
				if (evaluate) {
					ExpressionEvaluator ee = new ExpressionEvaluator(code,
							return_type, parameter_names, parameter_types);
					System.out.println("Evaluates to:"
							+ ee.execute(new Object[] {}));
				}
				System.out.println("\n");
			} else {
				i--;
			}
		}
	}

	public static class ExpressionEvaluator extends CodeEvaluator<Integer> {

		public ExpressionEvaluator(String code, Class<Integer> return_type,
				String[] parameter_names, Class<?>[] parameter_types) {
			super(code, return_type, parameter_names, parameter_types);
		}

		@Override
		public String getEndCode() {
			return "";
		}

		@Override
		public String getStartCode() {
			return "";
		}

	}
}
